﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace AJAXServiceProxyGeneration
{
    /// <summary>
    /// Writes Generated Code to Files
    /// </summary>
    public class GeneratorFileWriter : IGeneratorWriter
    {
        #region Contructor

        /// <summary>
        /// Contructor setting the OutputDirectory
        /// </summary>
        /// <param name="outputDirectory">The directory to which class files will be generated</param>        
        public GeneratorFileWriter(string outputDirectory)
        {
            this._outputPath = outputDirectory;
        }

        #endregion

        #region Properties

        private string _outputPath;

        /// <summary>
        /// Directory to which the files will be written
        /// </summary>
        private string OutputDirectory
        {
            get { return _outputPath; }
        }

        #endregion

        #region IGeneratorWriter Members

        /// <summary>
        /// Writes the <paramref name="stream"/> to a
        /// the specified <paramref name="fileName"/>
        /// </summary>
        /// <param name="fileName">Name of the file to write to</param>
        /// <param name="stream">Steam containign the file content</param>
        public void Write(string fileName,Stream stream)
        {
            string path = Path.Combine(
                    this.OutputDirectory,
                    string.Concat(fileName, ".cs"));
            stream.Seek(0, 0);
            using (StreamWriter sw = new StreamWriter(path))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    sw.Write(sr.ReadToEnd());
                }
            }
        }

        #endregion
    }
}
